<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/6/12
	 * Time: 下午8:54
	 */

	namespace app\store\admin;

	use app\admin\controller\Admin;
	use app\common\builder\ZBuilder;
	use app\store\model\CartModel;
	use app\store\model\CategoryModel;

	class Cart extends Admin
	{
		/**
		 * @return mixed
		 * @throws \think\Exception
		 * @throws \think\exception\DbException
		 */
		public function lists()
		{
			// 查询
			$map = $this->getMap();
			// 排序
			$order = $this->getOrder('goods_id desc');

			$data_list = CartModel::field('*,goods_id as id')->where($map)->order($order)->paginate();

			return ZBuilder::make('table')
				->setPageTitle('商品管理')
				->setSearch(['goods_name' => '商品名称'])// 设置搜索框
				->addColumns([ // 批量添加数据列
							   ['id', 'ID'],
							   ['goods_name', '商品名称', 'text'],
							   //['storeclass_id', '商品分类', 'text'],
							   ['storeclass_id', '分类名称', '', CategoryModel::where([])->column('id,gc_name')],
							   ['member_id', '商家名称', 'text', '', StoreUserModel::getSelect()],
							   ['store_pphone', '联系人', 'text'],
							   ['goods_image', '广告图', 'picture'],
							   //['goods_price', '原始商品价格', 'text'],
							   ['goods_price', '商品价格', 'text'],
							   ['goods_store_price', '商品优惠价格', 'text'],
							   ['royalty', '分红', 'text'],
							   ['goods_num', '库存', 'text'],
							   ['goods_click', '浏览数', 'text'],
							   ['store_collect_num', '收藏数', 'text'],
							   ['goods_commend', '推荐', 'text'],
							   ['goods_addtime', '添加时间', 'text'],

							   ['store_salenum', '销售数量', 'text'],
							   ['status', '是否上架', 'yesno'],
							   ['status', '是否上架', 'switch'],
							   ['right_button', '操作', 'btn']
				])
				->addTopButtons(['add', 'enable', 'disable', 'delete'])// 批量添加顶部按钮
				->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']])// 批量添加右侧按钮
				->addOrder('goods_id')
				->setRowList($data_list)// 设置表格数据
				->fetch(); // 渲染模板
		}

		/**
		 * 新增
		 * @return mixed
		 * @throws Exception
		 */
		public function add()
		{
			// 保存数据
			if ($this->request->isPost())
			{
				// 表单数据
				$data = $this->request->post();

				if (isset($data['feature']))
				{
					$data['feature'] = join(',', $data['feature']);
				}
				if ($CartModel = CartModel::create($data))
				{
					// 记录行为
					//action_log('CartModel_add', null, $CartModel['id'], session('admin_id'), $CartModel['id']);
					$this->success('新增成功', 'lists');
				} else
				{
					$this->error('新增失败');
				}
			}

			// 显示添加页面
			return ZBuilder::make('form')
				->setBtnTitle('back', '返回前一页')
				->addFormItems([
					['text', 'goods_name', '商品名称'],
					['select', 'storeclass_id', '分类名称', '', CategoryModel::where([])->column('id,gc_name')],
					['select', 'member_id', '商家名称', '', StoreUserModel::getSelect()],
					['image', 'goods_image', '广告图'],
					['images', 'goods_images', '轮播图'],
					['text', 'goods_price', '商品价格'],
					['text', 'goods_store_price', '商品优惠价格'],
					['text', 'royalty', '分红', '分红：例如：1.5  1.5元'],
					['ueditor', 'goods_body', '商品详情描述'],
					['text', 'goods_num', '库存'],
					['text', 'store_pphone', '联系电话'],
					['checkbox', 'feature', '栏目选择', '', StoreFeatureModel::getColumn('action,title')],
					['radio', 'goods_commend', '是否推荐', '', ['否', '是'], 1],
					['radio', 'status', '立即启用', '', ['否', '是'], 1],
				])
				->fetch();
		}

		/**
		 * 编辑
		 * @param null $id
		 * @return mixed
		 * @throws Exception
		 * @throws \think\exception\DbException
		 */
		public function edit($id = null)
		{
			if ($id === null)
				$this->error('缺少参数');

			// 保存数据
			if ($this->request->isPost())
			{
				// 表单数据
				$data = $this->request->post();
				$data['goods_id'] = $data['id'];
				if (isset($data['feature']))
				{
					$data['feature'] = join(',', $data['feature']);
				}

				unset($data['id']);

				if (CartModel::update($data, ['goods_id' => $data['goods_id']]))
				{
					//dump(Db::name('')->getLastSql());die;
					// 记录行为
					action_log('CartModel_edit', null, $id, session('admin_id'), $id);

					$this->success('编辑成功', 'lists');
				} else
				{
					$this->error('编辑失败');
				}
			}

			$info = CartModel::field('*,goods_id as id')->where(['goods_id' => $id])->find();

			// 显示编辑页面
			return ZBuilder::make('form')
				->addFormItems([
					['hidden', 'id'],
					['text', 'goods_name', '商品名称'],
					['select', 'storeclass_id', '分类名称', '', CategoryModel::where([])->column('id,gc_name')],
					['select', 'member_id', '商家名称', '', StoreUserModel::getSelect()],
					['image', 'goods_image', '广告图'],
					['images', 'goods_images', '轮播图'],
					['text', 'goods_price', '商品价格'],
					['text', 'goods_store_price', '商品优惠价格'],
					['text', 'royalty', '分红', '分红：例如：1.5  1.5元'],
					['ueditor', 'goods_body', '商品详情描述'],
					['text', 'goods_num', '库存'],
					['text', 'store_pphone', '联系电话'],
					['checkbox', 'feature', '栏目选择', '', StoreFeatureModel::getColumn('action,title')],
					['radio', 'goods_commend', '是否推荐', '', ['否', '是'], 1],
					['radio', 'status', '立即启用', '', ['否', '是'], 1]
				])
				->setFormData($info)
				->fetch();
		}

		/**
		 * 删除
		 * @param array $record
		 * @return mixed
		 */
		public function delete($record = [])
		{
			return $this->setStatus('delete', new CartModel);
		}

		/**
		 * 启用
		 * @param array $record
		 * @return mixed
		 */
		public function enable($record = [])
		{
			return $this->setStatus('enable', new CartModel);
		}

		/**
		 * 禁用
		 * @param array $record
		 * @return mixed
		 */
		public function disable($record = [])
		{
			return $this->setStatus('disable', new CartModel);
		}

		/**
		 * 设置状态：删除、禁用、启用
		 * @param string $type
		 * @param array $record
		 * @param null $Model
		 * @return mixed
		 */
		public function setStatus($type = '', $record = [], $Model = null)
		{
			$ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
			$link_title = CartModel::where('goods_id', 'in', $ids)->column('goods_name');
			if ($Model)
			{
				return parent::setStatus($type, ['CartModel_' . $type, null, 0, session('admin_id'), implode('、', $link_title)], $Model);
			} else
			{
				return parent::setStatus($type, ['CartModel_' . $type, null, 0, session('admin_id'), implode('、', $link_title)], new CartModel);

			}
		}

		/**
		 * 快速编辑
		 * @param array $record
		 * @param null $Model
		 */
		public function quickEdit($record = [], $Model = null)
		{
			$id = input('post.pk', '');
			$field = input('post.name', '');
			$value = input('post.value', '');
			$CartModel = CartModel::where('goods_id', $id)->value($field);
			$details = '字段(' . $field . ')，原值(' . $CartModel . ')，新值：(' . $value . ')';
			return parent::quickEdit(['link_edit', 'cms_link', $id, session('admin_id'), $details], new CartModel());
		}
	}